#region

using System;
using System.Collections.Generic;

#endregion

namespace iTunesArtworkApp.Core
{
    /// <summary>
    ///   Album Comparer for Albums. Used to remove duplicate albums that occur naturally in iTunesWrapper because
    ///   of the 1 to many relation between albums and songs.
    /// </summary>
    public class AlbumComparer : IEqualityComparer<Album>
    {
        #region Implementation of IEqualityComparer<Album>

        /// <summary>
        ///   Determines whether the specified objects are equal.
        /// </summary>
        /// <returns>
        ///   true if the specified objects are equal; otherwise, false.
        /// </returns>
        /// <param name = "x">The first object of type to compare.
        /// </param>
        /// <param name = "y">The second object of type to compare.
        /// </param>
        public bool Equals(Album x, Album y)
        {
            var albumNameCompare = StringComparer.InvariantCultureIgnoreCase.Compare(x.AlbumName, y.AlbumName);
            var artistCompare = StringComparer.InvariantCultureIgnoreCase.Compare(x.Artist, y.Artist);

            var equal = (albumNameCompare == 0) && (artistCompare == 0);
            return equal;
        }

        /// <summary>
        ///   Returns a hash code for the specified object.
        /// </summary>
        /// <returns>
        ///   A hash code for the specified object.
        /// </returns>
        /// <exception cref = "T:System.ArgumentNullException">The type of  is a reference type and is null.
        /// </exception>
        public int GetHashCode(Album album)
        {
            var combinedName = album.AlbumName + album.Artist;
            var hashCode = StringComparer.InvariantCultureIgnoreCase.GetHashCode(combinedName);

            return hashCode;
        }

        #endregion
    }
}